package pta07;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Account{
	private int balance;
	private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

	public Account(int balance) {
		super();
		this.balance = balance;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}
	synchronized void deposit(int money){
		/*balance+=money;
		notify();*/
		lock.lock();
		try{
			balance+=money;
			condition.signal();
		}finally {
			lock.unlock();
		}
	}
	synchronized void withdraw(int money){
		/*while(balance<money){
			try{
				wait();
			}catch (InterruptedException e) {
			  e.printStackTrace();
			}
		}
		balance-=money;
		notify();*/
	/*	while(balance<money){
			try{
				wait();
			}catch (InterruptedException e) {
			  e.printStackTrace();
			}
		}
		  if(balance>=money){
			  balance-=money;
		  }
		  if(balance<0) 
		        throw new IllegalStateException(balance+"");	
	}*/
		lock.lock();
		try{
			while(balance<money){
					try{
						condition.await();
					}catch (InterruptedException e) {
					  e.printStackTrace();
					}
				}
				balance-=money;
			}finally {
				lock.unlock();
		  } 
	}
}
public class pta744_Mutex {

}
